package com.doctor.web.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.doctor.common.core.domain.R;
import com.doctor.common.utils.DateUtils;
import com.doctor.common.utils.SecurityUtils;
import com.doctor.web.domain.vo.CommentsVo;
import com.doctor.web.domain.vo.CreateByVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.doctor.common.annotation.Log;
import com.doctor.common.core.controller.BaseController;
import com.doctor.common.core.domain.AjaxResult;
import com.doctor.common.enums.BusinessType;
import com.doctor.web.domain.Activity;
import com.doctor.web.service.IActivityService;
import com.doctor.common.utils.poi.ExcelUtil;
import com.doctor.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 活动管理Controller
 * 
 * @date 2024-05-24
 */
@RestController
@RequestMapping("/maincode/activity")
public class ActivityController extends BaseController
{
    @Autowired
    private IActivityService activityService;

    /**
     * 导出活动管理列表
     */
    @PreAuthorize("@ss.hasPermi('maincode:activity:export')")
    @Log(title = "活动管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,@RequestBody Activity activity)
    {
        List<Activity> list = activityService.selectActivityList(activity);
        ExcelUtil<Activity> util = new ExcelUtil<Activity>(Activity.class);
        util.exportExcel(response, list, "活动管理数据");
    }

    /**
     * 获取活动管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(activityService.selectActivityById(id));
    }

    /**
     * 新增活动管理
     */
    @Log(title = "活动管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Activity activity)
    {
        activity.setCreateTime(new Date());
        activity.setCreateBy(SecurityUtils.getUserId().toString());
        return toAjax(activityService.insertActivity(activity));
    }

    /**
     * 修改活动管理
     */
    @PreAuthorize("@ss.hasPermi('maincode:activity:edit')")
    @Log(title = "活动管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Activity activity)
    {
        return toAjax(activityService.updateActivity(activity));
    }

    /**
     * 删除活动管理
     */
    @PreAuthorize("@ss.hasPermi('maincode:activity:remove')")
    @Log(title = "活动管理", businessType = BusinessType.DELETE)
	@PostMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(activityService.deleteActivityByIds(ids));
    }

    /**
     * 查询活动管理列表
     */
    @PreAuthorize("@ss.hasPermi('maincode:activity:list')")
    @GetMapping("/list")
    public TableDataInfo list(@RequestBody Activity activity)
    {
        startPage();
        List<Activity> list = activityService.selectActivityList(activity);
        return getDataTable(list);
    }

    /**
     * 获取当前用户自己发布的所有活动
     */
    @GetMapping("/list/my")
    public TableDataInfo listMyActivities() {
        startPage();
        String userId = SecurityUtils.getUserId().toString();
        List<Activity> list = activityService.selectActivityListByCreateBy(userId);
        return getDataTable(list);
    }

    /**
     * 当前用户修改对应Id下的活动
     */
    @PostMapping("/edit/my")
    public AjaxResult editMy(@RequestBody Activity activity) //传activityId对应修改
    {
        return toAjax(activityService.updateActivity(activity));
    }

    /**
     * 用户获取最新发布活动列表(查询按 create_time 排序的
     */
    @GetMapping("/list/latest")
    public TableDataInfo listLatestActivities() {
        startPage();
        List<Activity> list = activityService.selectLatestActivities();
        return getDataTable(list);
    }

    /**
     * 用户获取今日发布活动列表(查询按 create_time 排序的
     */
    @GetMapping("/list/today")
    public TableDataInfo listTodayActivities() {
        startPage();
        Date todayStart = DateUtils.getBeginOfDay(new Date());
        Date todayEnd = DateUtils.getEndOfDay(new Date());
        List<Activity> list = activityService.selectActivitiesByDateRange(todayStart, todayEnd);
        return getDataTable(list);
    }

    /**
     * 用户获取本月发布活动列表(查询按 create_time 排序的
     */
    @GetMapping("/list/thisMonth")
    public TableDataInfo listThisMonthActivities() {
        startPage();
        Date monthStart = DateUtils.getBeginOfMonth(new Date());
        Date monthEnd = DateUtils.getEndOfMonth(new Date());
        List<Activity> list = activityService.selectActivitiesByDateRange(monthStart, monthEnd);
        return getDataTable(list);
    }

    /**
     * 用户获取地区选择活动
     */
    @GetMapping("/list/byLocation")
    public TableDataInfo listByLocation(@RequestParam String location) {
        startPage();
        Activity activity = new Activity();
        activity.setLocation(location);
        List<Activity> list = activityService.selectActivityList(activity);
        return getDataTable(list);
    }

    /**
     * 用户获取日期选择活动
     */
    @GetMapping("/list/byStartTime")
    public TableDataInfo listByStartTime(@RequestParam Date startTime) {
        startPage();
        Activity activity = new Activity();
        activity.setStartTime(startTime);
        List<Activity> list = activityService.selectActivityList(activity);
        return getDataTable(list);
    }

    /**
     * 上传文件到oss
     */
    @PostMapping("/uploadImages")
    public R saveImages(MultipartFile file) {
        String url = activityService.saveImages(file);
        return R.ok(url);
    }

    /**
     * 获取组织者create_by对应头像和用户昵称
     */
    @GetMapping("/getUserInfo/{id}")
    public R getUserInfo(@PathVariable("id") Long id) {
        CreateByVo createByVo = activityService.getUserInfo(id);
        return R.ok(createByVo);
    }

}
